package com.yloveg.impl;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.yloveg.dao.SPermissionMapper;
import com.yloveg.entity.SPermission;
import com.yloveg.entity.SPermissionExtend;
import com.yloveg.service.SPermissionService;

@Service
public class SPermissionServiceImpl implements SPermissionService {
 
    @Autowired
    private SPermissionMapper sPermissionMapper;

	@Override
	public int insertSPermission(SPermission sPermission) {
		// TODO Auto-generated method stub
		sPermission.setState(1);
		sPermission.setCreateDate(new Date());
		if(sPermission.getPermissionType().equals("0")){
			sPermission.setFatherId(1100000L);
		}
		return sPermissionMapper.insert(sPermission);
	}

	@Override
	public int updateByPrimaryKeySelective(SPermission sPermission) {
		// TODO Auto-generated method stub
		sPermission.setUpdateDate(new Date());
		return sPermissionMapper.updateByPrimaryKey(sPermission);
	}

	@Override
	public SPermission selectByPrimaryKey(Long id) {
		// TODO Auto-generated method stub
		return sPermissionMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<SPermissionExtend> getMem(SPermission sPermission) {
		return sPermissionMapper.getMem(sPermission);
	}

	@Override
	public List<SPermission> getUserMainMum(Long userId) {
		// TODO Auto-generated method stub
		return sPermissionMapper.getUserMainMum(userId);
	}

	@Override
	public List<SPermissionExtend> getMemF(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return sPermissionMapper.getMemF(map);
	}

	@Override
	@Cacheable(value="getUserMem",key="#userId")
	public Map<Long, List<SPermissionExtend>> getUserMainMumEche(Long userId) {
		// TODO Auto-generated method stub
		Map<String,Object> mapT = new HashedMap<String, Object>();
		mapT.put("userId", userId);
    	List<SPermission> mainMum =this.getUserMainMum(userId);
    	Map<Long,List<SPermissionExtend>> first = new HashMap<Long, List<SPermissionExtend>>();
		for (SPermission sPermission : mainMum) {
			mapT.put("fatherId", sPermission.getId());
			List<SPermissionExtend> memList2 = this.getMemF(mapT);
			for (SPermissionExtend sPermissionExtend2 : memList2) {
				mapT.put("fatherId", sPermissionExtend2.getId());
				List<SPermissionExtend> memList3 = this.getMemF(mapT);
				sPermissionExtend2.setChildren(memList3);
			}
			first.put(sPermission.getId(), memList2);
		}
		return first;
	}





 
 


}
